package 实验报告.实验2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class gui extends JFrame {
    public int score;
    public JTextField s;
    public boolean flag=false;
    public boolean iswait=false;
    private 打字游戏 function;
    public JLabel text;

    public gui(打字游戏 a){
        this.function=a;
        this.score=0;
        setSize(500,320);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addbutton();
        settext();
        setOther();
        setVisible(true);
    }
    private void setOther(){
        JPanel jp=new JPanel();
        jp.setBounds(0,240,500,30);
        jp.setLayout(new GridLayout(1,3));
        //标签
        JLabel jl=new JLabel("输入标签所显示的字母后回车");
        //文本框
        JTextField jt=new JTextField("",3);
        //输入监听
        jt.addActionListener((a)->{
            String str=jt.getText();
            if(!iswait&&flag&&str.equals(text.getText())){
                flag=false;
                score++;
                s.setText("得分："+score);
            }
            jt.setText("");
        });
        //得分
        s=new JTextField(8);
        s.setBackground(Color.YELLOW);
        s.setEditable(false);
        s.setText("得分：0");
        jp.add(jl);
        jp.add(jt);
        jp.add(s);
        this.add(jp);
    }
    private void settext(){
        text=new JLabel();
        text.setFont(new Font("宋体",Font.PLAIN,170));
        text.setBounds(180,40,500,200);
        this.add(text);
    }
    private void addbutton(){
        JButton b=new JButton("开始");
        Font f=new Font("宋体",Font.PLAIN,20);
        b.setFont(f);
        b.setBounds(0,0,500,40);
        Object t=this;
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e){
                    if(b.getText().equals("开始")){
                        b.setText("暂停");
                        if(!iswait)
                            function.fun((gui)t);
                        else{
                            iswait=false;
                        }
                    }
                    else {
                        b.setText("开始");
                        iswait=true;
                    }
                }
            });
        this.add(b,BorderLayout.NORTH);
    }
}
